package com.fhz.shop.ui.dashboard.left;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.fhz.shop.R;
import com.fhz.shop.base.BaseFragment;
import com.fhz.shop.callback.ApiResponse;
import com.fhz.shop.databinding.FragmentHomeBinding;
import com.fhz.shop.databinding.FragmentLeftBinding;
import com.fhz.shop.entity.Category;
import com.fhz.shop.ui.dashboard.DashboardViewModel;
import com.fhz.shop.ui.home.HomeViewModel;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class LeftFragment extends BaseFragment<DashboardViewModel, FragmentLeftBinding> {

    DashLeftRecyclerViewAdapter mDashLeftRecyclerViewAdapter;

    List<Category> mCategories;
    @Override
    protected void initData() {

        setRVAdapter();


        viewModel.getCategory(0).observe(this, new Observer<ApiResponse<List<Category>>>() {
            @Override
            public void onChanged(ApiResponse<List<Category>> listApiResponse) {
                System.out.println(listApiResponse.getData().size());
                mCategories.addAll(listApiResponse.getData());
                mDashLeftRecyclerViewAdapter.notifyDataSetChanged();
            }
        });

        mDashLeftRecyclerViewAdapter.setOnItemClickListener((v, position) -> {
            //两个fragment怎么传值
            //eventbus  -> 事件总线 // livedata 时间总线
            //viewMdoel

            EventBus.getDefault().post(position);
        });

    }

    private void setRVAdapter() {
        mCategories = new ArrayList<>();
        mDashLeftRecyclerViewAdapter = new DashLeftRecyclerViewAdapter(getContext(),mCategories);
        binding.leftRecyclerView.setAdapter(mDashLeftRecyclerViewAdapter);
        binding.leftRecyclerView.setLayoutManager(new LinearLayoutManager(getContext(), RecyclerView.VERTICAL,false));

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_left;
    }

    @Override
    protected DashboardViewModel createViewModel() {
        return new ViewModelProvider(getParentFragment()).get(DashboardViewModel.class);
    }
}